Skip to content

Python: Add file_ids support and per-message file attachment for Azure AI code interpreter#4201

Open
giles17 wants to merge 10 commits intomicrosoft:mainfrom
giles17:azure_ai_code_int
Open

Python: Add file_ids support and per-message file attachment for Azure AI code interpreter#4201
giles17 wants to merge 10 commits intomicrosoft:mainfrom
giles17:azure_ai_code_int

Conversation

@giles17
Copy link
Contributor

@giles17 giles17 commented Feb 23, 2026

Summary

Adds file_ids and data_sources support to get_code_interpreter_tool() on both V1 (AzureAIAgentClient) and V2 (AzureAIClient) clients, and adds per-message file attachment support via Content.from_hosted_file() for the V1 client.

Problem

  1. get_code_interpreter_tool() accepted no parameters on either client — users had no way to attach files for code interpreter analysis through the framework's factory method.
  2. AzureAIAgentClient._prepare_messages() had no hosted_file handling, so Content.from_hosted_file() was silently dropped. This meant per-message file scoping (via MessageAttachment) was not possible.

Changes

Tool-level file_ids (both clients)

  • _chat_client.py (V1): get_code_interpreter_tool() now accepts file_ids: list[str | Content] and data_sources: list[VectorStoreDataSource], forwarding to the SDK's CodeInterpreterTool.
  • _client.py (V2): Same file_ids: list[str | Content] widening, using CodeInterpreterToolAuto(file_ids=...).
  • _shared.py: Added resolve_file_ids() helper to extract file IDs from both str and Content.from_hosted_file() objects.
  • file_ids accepts Content.from_hosted_file() objects alongside plain strings for ergonomic usage.

Per-message file attachment (V1 client)

  • _chat_client.py: Added hosted_file case in _prepare_messages() that converts Content.from_hosted_file() into MessageAttachment on ThreadMessageOptions. This matches the underlying Azure AI Agents SDK MessageAttachment pattern for per-message file scoping.

Tests

  • test_azure_ai_agent_client.py: Added tests for file_ids, data_sources, mutual exclusivity, Content objects, mixed types, unsupported Content types, and 3 tests for hosted_file → MessageAttachment conversion.
  • test_azure_ai_client.py: Added Content tests for V2 client.
  • test_agent_provider.py: Added tests for tool_resources flow.

Sample

  • azure_ai_with_code_interpreter_file_upload.py: New sample demonstrating per-message CSV file attachment with V1 client using Content.from_hosted_file().
  • employees.csv: Test data for the sample.

Usage

Tool-level (file shared across thread)

tool = client.get_code_interpreter_tool(file_ids=["file-abc123"])
agent = await provider.create_agent(tools=[tool], ...)
response = await agent.run("Analyze the CSV.")

Per-message (file scoped to single message)

file_content = Content.from_hosted_file(file_id=uploaded.id)
message = Message(role="user", contents=["Analyze the CSV.", file_content])
response = await agent.run(message)

Fixes #4050

…et_code_interpreter_tool()

Update the factory method to accept file_ids and data_sources keyword
arguments, matching the underlying azure.ai.agents SDK CodeInterpreterTool
constructor. This enables users to attach uploaded files for code
interpreter analysis.

Fixes microsoft#4050

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 23, 2026 23:34
@giles17 giles17 added bug Something isn't working python labels Feb 23, 2026
@giles17 giles17 changed the title Python: Add file_ids and data_sources support to AzureAIAgentClient.get_code_interpreter_tool() Python: Add file_ids support to AzureAIAgentClient.get_code_interpreter_tool() Feb 23, 2026
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 23, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/azure-ai/agent_framework_azure_ai
   _chat_client.py4827584%416–417, 419, 603, 608–609, 611–612, 615, 618, 620, 625, 886–887, 889, 892, 895, 898–903, 906, 908, 916, 928–930, 934, 937–938, 946–949, 959, 967–970, 972–973, 975–976, 983, 991–992, 1000–1001, 1006–1007, 1011–1018, 1023, 1026, 1034, 1040, 1048–1050, 1053, 1075–1076, 1209, 1237, 1252, 1377, 1499
   _client.py3674587%388, 390, 433, 441–453, 466, 526, 541–546, 589, 624, 626, 647–648, 651, 692, 695, 697, 788, 819, 861, 1070, 1073, 1076–1077, 1079–1082, 1125
   _shared.py2472291%110, 143, 179, 227, 229–231, 263, 306, 318–320, 349, 351, 353, 357, 422, 453, 494, 501, 513, 532
TOTAL22193276387% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
4686 247 💤 0 ❌ 0 🔥 1m 20s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds file_ids and data_sources support to AzureAIAgentClient.get_code_interpreter_tool(), enabling users to attach uploaded files to the code interpreter for analysis. This addresses issue #4050 where users couldn't provide file attachments through the framework's factory method.

Changes:

  • Extended get_code_interpreter_tool() to accept file_ids and data_sources keyword arguments that are forwarded to the Azure Agents SDK's CodeInterpreterTool
  • Added VectorStoreDataSource import to support the new parameter type
  • Added comprehensive test coverage for basic instantiation, file_ids forwarding, and integration with the tool preparation pipeline

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py Added file_ids and data_sources parameters to get_code_interpreter_tool() method with updated docstring and examples; imported VectorStoreDataSource type
python/packages/azure-ai/tests/test_azure_ai_agent_client.py Added three test cases: basic tool creation, file_ids parameter forwarding, and tool_resources population in run options

giles17 and others added 5 commits February 24, 2026 11:20
Add hosted_file handling in _prepare_messages() to convert
Content.from_hosted_file() into MessageAttachment on ThreadMessageOptions.
This enables per-message file scoping for code interpreter, matching the
underlying Azure AI Agents SDK MessageAttachment pattern.

- Add hosted_file case in _prepare_messages() match statement
- Import MessageAttachment from azure.ai.agents.models
- Add sample for per-message CSV file attachment with code interpreter
- Add employees.csv test data file
- Add 3 unit tests for hosted_file attachment conversion

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giles17 giles17 changed the title Python: Add file_ids support to AzureAIAgentClient.get_code_interpreter_tool() Python: Add file_ids support and per-message file attachment for Azure AI code interpreter Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Not able to read attached files when carryout simple analysis with csv files

4 participants